import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RadioButtonExample extends JFrame implements ActionListener {

    private JRadioButton radioButton1;
    private JRadioButton radioButton2;
    private JRadioButton radioButton3;

    public RadioButtonExample() {
        // 创建选项 1、选项 2 和选项 3 的单选按钮
        radioButton1 = new JRadioButton("选项 1");
        radioButton2 = new JRadioButton("选项 2");
        radioButton3 = new JRadioButton("选项 3");

        // 将单选按钮添加到 ButtonGroup 中
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        // 添加单选按钮到面板中
        JPanel panel = new JPanel();
        panel.add(radioButton1);
        panel.add(radioButton2);
        panel.add(radioButton3);

        // 设置监听器
        radioButton1.addActionListener(this);
        radioButton2.addActionListener(this);
        radioButton3.addActionListener(this);

        // 将面板添加到窗口中
        add(panel);

        // 设置窗口属性
        setSize(300, 150);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JRadioButton radioButton = (JRadioButton) e.getSource();

        // 显示消息对话框并设置单选按钮的可见性
        JOptionPane.showMessageDialog(this, radioButton.getText() + " 被选中了！");
        radioButton.setVisible(false);
        for (JRadioButton rb : getRadioButtons()) {
            if (!rb.equals(radioButton)) {
                rb.setVisible(true);
            }
        }
    }

    private JRadioButton[] getRadioButtons() {
        return new JRadioButton[]{radioButton1, radioButton2, radioButton3};
    }

    public static void main(String[] args) {
        new RadioButtonExample();
    }
}
